package com.zx.tradeclient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index1.Navigation;
import com.zx.tradeclient.entity.UpdateInfo;

public class StartActivity extends Activity {
	private List<Navigation> navs = new ArrayList<Navigation>();
	private UpdateInfo up_info = new UpdateInfo();
	private TradeApp app;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startactivity);

		ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		app = (TradeApp) getApplication();
		app.isNetWorkConnection = HttpUtils.checkConnectivity(connectionManager);

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "newsClassList");
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);
			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(StartActivity.this, "网络超时", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				StartActivity.this.finish();
			}

		});

		ActivityTaskManager.getInstance().putActivity("StartActivity", this);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityTaskManager.getInstance().removeActivity("StartActivity");
	}

	private void parseJson(JSONObject response) {
		try {
			String succeed = response.getString("succeed");
			if (succeed.equals("000")) {
				navs.clear();
				JSONArray datalist = response.getJSONArray("dataList");
				for (int i = 0; i < datalist.length(); i++) {
					navs.add(new Navigation(datalist.getJSONObject(i).getString("id"), datalist.getJSONObject(i).getString("name"), datalist.getJSONObject(i)
							.getInt("ncount")));
				}
				app.companySlideCount = response.getInt("companySlideCount");
				app.productSlideCount = response.getInt("productSlideCount");
				app.setNavs(navs);

			} else {

			}
			// String totalPage = new
			// JSONObject(str).getString("totalPage");
		} catch (JSONException e) {
			e.printStackTrace();
			Toast toast = Toast.makeText(StartActivity.this, "网络超时", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			StartActivity.this.finish();
		} finally {
			// new CartageTask().execute(URLHelper.mPrefix);
			Map<String, String> map = new HashMap<String, String>();
			map.put("act", "categoryInfo");
			HttpUtils.post(map, new CallBack() {

				@Override
				public void onSuccess(JSONObject response) {
					parseCategoryJson(response);
				}

				@Override
				public void onFailure(Throwable e) {
					Toast toast = Toast.makeText(StartActivity.this, "网络超时", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					StartActivity.this.finish();
				}

			});
		}
	}

	private void parseCategoryJson(JSONObject response) {
		try {
			JSONArray datalist;
			JSONArray second;
			String succeed = response.getString("succeed");
			if (succeed.equals("000")) {

				datalist = response.getJSONArray("provinceCityList");
				if(Constant.debug)Log.d("TAG", response.toString());
				app.provinces = new String[datalist.length()];
				app.citys = new String[datalist.length()][];
				app.cityids = new String[datalist.length()][];
				for (int i = 0; i < datalist.length(); i++) {
					app.provinces[i] = datalist.getJSONObject(i).getString("name");
					second = datalist.getJSONObject(i).getJSONArray("seconds");
					app.citys[i] = new String[second.length()];
					app.cityids[i] = new String[second.length()];
					for (int j = 0; j < second.length(); j++) {
						app.citys[i][j] = second.getJSONObject(j).getString("name");
						app.cityids[i][j] = second.getJSONObject(j).getString("id");
					}
				}

				datalist = response.getJSONArray("productTypeList");
				// Log.d("TAG", "fuck");
				app.productgroup = new String[datalist.length()];
				app.producttypes = new String[datalist.length()][];
				app.typeids = new String[datalist.length()][];
				for (int i = 0; i < datalist.length(); i++) {
					app.productgroup[i] = datalist.getJSONObject(i).getString("name");
					second = datalist.getJSONObject(i).getJSONArray("seconds");
					app.producttypes[i] = new String[second.length()];
					app.typeids[i] = new String[second.length()];
					for (int j = 0; j < second.length(); j++) {
						app.producttypes[i][j] = second.getJSONObject(j).getString("name");
						app.typeids[i][j] = second.getJSONObject(j).getString("id");
					}
				}

				datalist = response.getJSONArray("tradeWayList");
				// Log.d("TAG", "fuck");
				app.tradeways = new String[datalist.length()];
				app.tradewaysid = new String[datalist.length()];
				for (int i = 0; i < datalist.length(); i++) {
					app.tradeways[i] = datalist.getJSONObject(i).getString("name");
					app.tradewaysid[i] = datalist.getJSONObject(i).getString("id");

				}
				datalist = response.getJSONArray("orderStatusList");
				// Log.d("TAG", "fuck");
				app.ordertypes = new String[datalist.length()];
				app.ordertypesid = new String[datalist.length()];
				for (int i = 0; i < datalist.length(); i++) {
					app.ordertypes[i] = datalist.getJSONObject(i).getString("name");
					app.ordertypesid[i] = datalist.getJSONObject(i).getString("id");

				}

			} else {

			}

		} catch (JSONException e) {
			e.printStackTrace();
			Toast toast = Toast.makeText(StartActivity.this, "网络超时", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			StartActivity.this.finish();
		} finally {
			Intent intent = new Intent(StartActivity.this, MainActivity.class);
			intent.putExtra("info", up_info);
			StartActivity.this.startActivity(intent);
			StartActivity.this.finish();
		}
	}

}
